<template>
  <div class="app-container">
    <el-row :gutter="20" >
     <!--部门数据-->
     <el-col :span="9" :xs="24" :gutter="10">
      <el-row>
        <el-col :span="1.5">
          <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-if="refreshTable" v-loading="loading" :data="warehouseDeptList" row-key="warehouseDeptId" 
        :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" >
        <el-table-column label="仓库名称" align="center" prop="warehouseDeptName"   >
          <template slot-scope="scope">
            <span v-if="scope.row.warehouseDeptOrderNum ==3">
                        <span  @click="handleNodeClick(scope.row)">{{scope.row.warehouseDeptName}}</span>
                      </span>
                      <span >{{scope.row.warehouseDeptName}}</span>
                    </template>
          </el-table-column>
        <el-table-column label="仓库状态" align="center" prop="warehouseDeptStatus">
          <template slot-scope="scope">
            <span v-if="scope.row.warehouseDeptOrderNum ==3">
            <span v-if="scope.row.warehouseDeptStatus == 0">
              <el-tag type="success">正常</el-tag>
            </span>
            <span v-if="scope.row.warehouseDeptStatus == 1">
              <el-tag type="danger">占用</el-tag>
            </span>
            </span>
          </template>
        </el-table-column>
        
      </el-table>

      </el-col>
      <el-col :span="15" :xs="24">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      
      <el-form-item label="单据号:" prop="warehouseStockOrder">
        <span style="padding-right: 55px; font-size: 15px">{{
          stock.warehouseStockOrder
        }}</span>
      </el-form-item>
      <el-form-item label="单据日期" prop="warehouseStockDate">
        <span style="padding-right: 55px; font-size: 15px">{{
          stock.warehouseStockDate
        }}</span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['stock:stock:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['stock:stock:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleGoProduct"
          >申领</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="仓库商品id" align="center" prop="warehouseStockTbId" />
      <el-table-column label="库存id" align="center" prop="warehouseStockId" /> -->
      <el-table-column label="存货编码" align="center" prop="warehouseStockPo" />
      <el-table-column label="库存名称" align="center" prop="warehouseStockName"  width="150px"/>
      <el-table-column label="规格型号" align="center" prop="warehouseStockSpeion" />
      <el-table-column label="主计量单位" align="center" prop="warehouseStockUnit" />
      <el-table-column label="库存数量" align="center" prop="warehouseStockNum" />
      <el-table-column label="单价" align="center" prop="warehouseStockPrice" />
      <el-table-column label="金额" align="center" prop="warehouseStockTotalPrice" />
      <el-table-column label="税额" align="center" prop="warehouseStockTaxPrice" />
      <el-table-column label="含税总金额" align="center" prop="warehouseStockUnitTotalPrice" />
    
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    

    
    </el-col>
    </el-row>
    <!-- 添加或修改库存管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库id" prop="warehouseDeptId">
          <el-input v-model="form.warehouseDeptId" placeholder="请输入仓库id" />
        </el-form-item>
        <el-form-item label="单据号" prop="warehouseStockOrder">
          <el-input v-model="form.warehouseStockOrder" placeholder="请输入单据号" />
        </el-form-item>
        <el-form-item label="单据日期" prop="warehouseStockDate">
          <el-date-picker clearable v-model="form.warehouseStockDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择单据日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { listTableStock, getTableStock, delTableStock, addTableStock, updateTableStock } from "@/api/stock/table";
import { listStock, getStock, delStock, addStock, updateStock } from "@/api/stock/stock";
import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listWarehouseDept } from "@/api/warehouseDept/warehouseDept";
export default {
  name: "Stock",
  data() {
    return {
      warehouseDeptList:[],
      tableList:[],
           // 是否展开，默认全部展开
           isExpandAll: true,
      formWarehouse:{},
    // 仓库分类树选项
    warehouseDeptOptions: [],
    stock:{},
       // 重新渲染表格状态
       refreshTable: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
     
      // 库存管理表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams:{},
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        warehouseDeptId: null,
        warehouseStockOrder: null,
        warehouseStockType: null,
        warehouseStockDate: null,
      },
      queryTable:{
        pageNum: 1,
        pageSize: 10,
        warehouseStockId: null,
        warehouseStockPo: null,
        warehouseStockName: null,
        warehouseStockUnit: null,
        warehouseStockNum: null,
        warehouseStockPrice: null,
        warehouseStockTotalPrice: null,
        warehouseStockTaxPrice: null,
        warehouseStockUnitTotalPrice: null
      },
      
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getWarehouseList();   
  },
  mounted(){
  },
  methods: {
     /** 查询库存商品列表 */
     
     getTableList() {
      this.queryTable.warehouseStockId=this.stock.warehouseStockId
      console.log( this.queryTable.warehouseStockId)
      if(this.queryTable.warehouseStockId !==null){
        listTableStock(this.queryTable).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
      
      });
      }
      this.tableList =null
     
    },
    handleGoProduct(){

    },
   
       // 节点单击事件
    handleNodeClick(row) {
      this.queryParam.warehouseDeptId = row.warehouseDeptId;
      this.getList();
    },
     /** 查询仓库分类列表 */
     getWarehouseList() {
        this.loading = true;
        listWarehouseDept(this.queryParams).then(response => {
          this.warehouseDeptList = this.handleTree(response.data, "warehouseDeptId", "warehouseDeptParentId");
          this.loading = false;
        });
      },
      /** 转换仓库分类数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.warehouseDeptId,
          label: node.warehouseDeptAncestors,
          children: node.children
        
        };
     
      },
    /** 展开/折叠操作 */
    toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      convertObject() {
        //初始化
        this.stock.warehouseStockId=null
      this.stock.warehouseStockDate=null
      this.stock.warehouseStockOrder=null
      //当没有被点击时 初始化
        if(this.queryParam.warehouseDeptId==null||this.queryParam.warehouseStockId){

          return 0;
        }
        //将数组传入 stock对象中 数组转换对象
        let stock = {}
      this.stockList.forEach(item => {
      stock[item.warehouseStockOrder] = item.warehouseStockOrder
      stock[item.warehouseStockDate] = item.warehouseStockDate
      stock[item.warehouseStockId] = item.warehouseStockId
      this.stock.warehouseStockId=item.warehouseStockId
      this.stock.warehouseStockDate=item.warehouseStockDate
      this.stock.warehouseStockOrder=item.warehouseStockOrder  
      
    })
    
    return stock
    
  },
    /** 查询库存管理列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParam).then(response => {
        this.stockList = response.rows;
        //将对象转换数组
        this.convertObject();
         //获取商品信息
        this.getTableList();
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        warehouseStockId: null,
        warehouseDeptId: null,
        warehouseStockOrder: null,
        warehouseStockType: null,
        warehouseStockDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warehouseStockId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const warehouseStockId = row.warehouseStockId || this.ids
      getStock(warehouseStockId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warehouseStockId != null) {
            updateStock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStock(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warehouseStockIds = row.warehouseStockId || this.ids;
      this.$modal.confirm('是否确认删除库存管理编号为"' + warehouseStockIds + '"的数据项？').then(function () {
        return delStock(warehouseStockIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('stock/stock/export', {
        ...this.queryParams
      }, `stock_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
  